package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.lamp.care.entity.BPgSrxDetailNormal;
import top.tangyh.lamp.care.mapper.BPgSrxDetailNormalMapper;
import top.tangyh.lamp.care.mapper.BPgSrxMapper;
import top.tangyh.lamp.care.service.BPgSrxService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BPgSrxManager;
import top.tangyh.lamp.care.entity.BPgSrx;
import top.tangyh.lamp.care.vo.save.BPgSrxSaveVO;
import top.tangyh.lamp.care.vo.save.PgRecordDetailVo;
import top.tangyh.lamp.care.vo.save.SrxDetailNormalVO;
import top.tangyh.lamp.care.vo.save.SrxVO;
import top.tangyh.lamp.care.vo.update.BPgSrxUpdateVO;
import top.tangyh.lamp.care.vo.result.BPgSrxResultVO;
import top.tangyh.lamp.care.vo.query.BPgSrxPageQuery;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 评估评分输入项
 * </p>
 *
 * @author zqb
 * @date 2024-07-09 17:07:17
 * @create [2024-07-09 17:07:17] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BPgSrxServiceImpl extends SuperServiceImpl<BPgSrxManager, Long, BPgSrx> implements BPgSrxService {

    private final BPgSrxMapper bPgSrxMapper;

    private final BPgSrxDetailNormalMapper bPgSrxDetailNormalMapper;


    @Override
    public List<BPgSrx> selectByPgid(Long pgid){
        QueryWrapper<BPgSrx> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BPgSrx::getPgId,pgid);
        List<BPgSrx> pgSrxPOS = bPgSrxMapper.selectList(wrapper);
        return pgSrxPOS;
    }

    @Override
    public SrxVO selectBySrxId(Long srxId) {
        BPgSrx bPgSrx = bPgSrxMapper.selectById(srxId);
        if(bPgSrx == null) {
            return null;
        }
        SrxVO srxVO = BeanUtil.toBean(bPgSrx, SrxVO.class);
        List<BPgSrxDetailNormal> detailNormalPOS = bPgSrxDetailNormalMapper.selectListBySrxId(srxId);
        if(CollectionUtil.isNotEmpty(detailNormalPOS)) {
            List<SrxDetailNormalVO> detailNormalVOS =BeanUtil.copyToList(detailNormalPOS, SrxDetailNormalVO.class);
            srxVO.setNormalDetailList(detailNormalVOS);
        }
        return srxVO;
    }

    @Override
    public List<SrxVO> selectBySrxIds(Set<Long> srxids) {
        List<BPgSrx> yxPgSrxPOs = bPgSrxMapper.selectByIds(srxids);
        if(CollectionUtil.isNotEmpty(yxPgSrxPOs)) {
            List<BPgSrxDetailNormal> detailNormalPOS = bPgSrxDetailNormalMapper.selectListBySrxIds(srxids);
            List<SrxVO> srxVOs =BeanUtil.copyToList(yxPgSrxPOs, SrxVO.class);
            if(CollectionUtil.isNotEmpty(srxVOs)){
                Map<Long,List<SrxDetailNormalVO>> datamap = null;
                if(CollectionUtil.isNotEmpty(detailNormalPOS)) {
                    List<SrxDetailNormalVO> sdvos = BeanUtil.copyToList(detailNormalPOS, SrxDetailNormalVO.class);
                    datamap = sdvos.stream().collect(Collectors.groupingBy(SrxDetailNormalVO::getPgSrxId));
                }
                if(null == datamap){
                    datamap = new HashMap<>();
                }
                for(SrxVO srxVO : srxVOs){
                    srxVO.setNormalDetailList(datamap.get(srxVO.getId()));
                }
            }
            return srxVOs;
        }
        return null;
    }

}


